package com.example.demo.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.BaseErrorConfigEnum;
import com.example.demo.common.BasePageResponse;
import com.example.demo.common.ResultObject;
import com.example.demo.common.TerminologyConfigEnum;
import com.example.demo.dao.IVisaCardDao;
import com.example.demo.dao.IVpsDao;
import com.example.demo.jooq.tables.daos.VisaCardDao;
import com.example.demo.jooq.tables.pojos.VisaCard;
import com.example.demo.model.po.VisaPage;
import com.example.demo.model.po.VpsPage;
import com.example.demo.model.request.VisaPageRequest;
import com.example.demo.model.request.VisaRequest;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.model.response.VisaResponse;
import com.example.demo.model.response.VpsResponse;
import com.example.demo.service.BaseBusinessServiceImpl;
import com.example.demo.service.order.IVisaCardService;
import com.example.demo.service.order.IVpsService;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.ClazzUtils;
import com.example.demo.util.DateUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IVisaCardServiceImpl extends BaseBusinessServiceImpl implements IVisaCardService {
    @Autowired
    private IVisaCardDao iVisaCardDao;
    @Autowired
    private VisaCardDao visaCardDao;

    @Override
    public ResultObject<BasePageResponse<VisaResponse>> queryPageCardList(VisaPageRequest visaPageRequest) {
        ResultObject<BasePageResponse<VisaResponse>> responseResultObject = new ResultObject<>();
        List<VisaPage> vpsPages =  iVisaCardDao.queryPageVisaList(visaPageRequest);
        if(AssertUtils.isNotEmpty(vpsPages)){
            Integer integer =vpsPages.get(0).getTotalLine();
            List<VisaResponse> vpsResponses = (List<VisaResponse>)this.converterList(vpsPages,new TypeToken<List<VisaResponse>>() {}.getType() );
            //术语转换
            vpsResponses.forEach(visaResponse -> {
                if(AssertUtils.isNotNull(visaResponse.getVisaType())){
                    Arrays.stream(TerminologyConfigEnum.CARD_TYPE.values()).filter(value->value.name().equals(visaResponse.getVisaType())).forEach(value->visaResponse.setVisaType(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getVisaChannel())){
                    Arrays.stream(TerminologyConfigEnum.CARD_CHANNEL.values()).filter(value->value.name().equals(visaResponse.getVisaChannel())).forEach(value->visaResponse.setVisaChannel(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getCardStatus())){
                    Arrays.stream(TerminologyConfigEnum.CARD_STATUS.values()).filter(value->value.name().equals(visaResponse.getCardStatus())).forEach(value->visaResponse.setCardStatus(value.desc()));
                }
            });
            BasePageResponse<VisaResponse> responseBasePageResponse =BasePageResponse.getData(visaPageRequest.getCurrentPage(),visaPageRequest.getPageSize(),integer,vpsResponses);
            responseResultObject.setData(responseBasePageResponse);
        }
        return responseResultObject;
    }

    @Override
    public ResultObject saveCard(VisaRequest visaRequest) {
        this.getLogger().info("保存虚拟卡,请求数据为:{}", JSON.toJSONString(visaRequest));
        ResultObject resultObject = new ResultObject();
        //数据校验
        vaildVisaCardParameter(visaRequest);
        VisaCard card=null;
        if(AssertUtils.isNotNull(visaRequest.getVisaCardId())){
            card=visaCardDao.fetchOneByVisaCardId(visaRequest.getVisaCardId());
            AssertUtils.isNotNull(this.getLogger(),card,BaseErrorConfigEnum.CARD_NOT_NULL);
        }else {
            card=new VisaCard();
        }
        ClazzUtils.copyPropertiesIgnoreNull(visaRequest,card);
        //日期特殊设置
        if(AssertUtils.isNotNull(visaRequest.getEndDate())){
            card.setEndDate(DateUtils.stringToTime(visaRequest.getEndDate(),DateUtils.FORMATE2));
        }
        //持久化
        if(AssertUtils.isNotEmpty(visaRequest.getVisaCardId())){
            card.setUpdatedDate(System.currentTimeMillis());
            visaCardDao.update(card);
        }else {
            card.setCardStatus(AssertUtils.isNotNull(visaRequest.getCardStatus())?visaRequest.getCardStatus():TerminologyConfigEnum.EMAIL_STATUS.NEW.name());
            card.setVisaCardId(DateUtils.getJobNumberByTime2("CARD",null,DateUtils.FORMATE53,false));
            card.setCreatedDate(System.currentTimeMillis());
            card.setUpdatedDate(System.currentTimeMillis());
            visaCardDao.insert(card);
        }
        return resultObject;
    }

    private void vaildVisaCardParameter(VisaRequest visaRequest) {
        AssertUtils.isNotNull(this.getLogger(),visaRequest.getVisaCardNo(), BaseErrorConfigEnum.CARD_NO_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),visaRequest.getVisaType(), BaseErrorConfigEnum.CARD_TYPE_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),visaRequest.getVisaChannel(), BaseErrorConfigEnum.CARD_CHANNEL_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),visaRequest.getBalanceAmount(), BaseErrorConfigEnum.BALANCE_AMOUNT_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),visaRequest.getEndDate(), BaseErrorConfigEnum.BALANCE_AMOUNT_NOT_NULL);
        //AssertUtils.isNotNull(this.getLogger(),visaRequest.getCvc(),BaseErrorConfigEnum.CARD_CVC_IS_NOT_NULL);
    }

    @Override
    public ResultObject<VisaResponse> queryCardDetail(String cardId) {
        ResultObject<VisaResponse> resultObject = new ResultObject();
        VisaCard visaCard = visaCardDao.fetchOneByVisaCardId(cardId);
        if(AssertUtils.isNotNull(visaCard)){
            VisaResponse visaResponse = (VisaResponse)this.converterObject(visaCard,VisaResponse.class);
            resultObject.setData(visaResponse);
        }
        return resultObject;
    }
}
